package GUI.List;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.*;

import javax.swing.*;
import javax.swing.event.*;

import GUI.TabPanel.FileView;

public class FileList extends JScrollPane {

	private JList<String> list = new JList<String>();
	private DefaultListModel<String> Model = new DefaultListModel<String>();
	private String Mac_ID;
	private FileView view;
	
	private void getFiles() {
		Model.clear();
		File tmp = new File(Mac_ID + "/Data/");
		if(tmp.exists()) {
			File name[] = tmp.listFiles();
			for (File tmps : name) {
				if(tmps.isFile()) {
					String filename = tmps.getName();
					if(filename.substring(filename.lastIndexOf(".") + 1).equalsIgnoreCase("csv")) {
						Model.addElement(filename);
					}
				}
			}
		}
	}
	
	public void setView(FileView view) {
		this.view = view;
	}
	
	private void initList() {
		list.setModel(Model);
		list.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent e) {
				if(e.getClickCount() == 2) {
					TwoClick(list.getSelectedValue());
				}
			}
		});
	}
	
	private void TwoClick(String tmp) {
		this.view.reFresh(tmp);
	}
	
	public void reFresh(String Mac_ID) {
		this.Mac_ID = Mac_ID;
		getFiles();
	}
	
	public FileList() {
		initList();
		this.setViewportView(list);
	}
}
